package com.baozh.resource;

import com.baozh.data.StoreData;
import com.baozh.model.AuthenticationInfo;
import com.baozh.model.Order;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.*;

/**
 * @author hubert
 * @date 2018/12/14
 * @description PetStoreResource
 */
@Path("/store")
@Produces({"application/json", "application/xml"})
public class PetStoreResource {
	static StoreData storeData = new StoreData();

	@GET
	@Path("/order/{orderId}")
	@Operation(
			summary = "Find purchase order by ID",
			description = "For valid response try integer IDs with value >= 1 and <= 10. Other values will generated exceptions",
			responses = {
					@ApiResponse(content = @Content(schema = @Schema(implementation = Order.class))),
					@ApiResponse(responseCode = "400", description = "Invalid ID supplied"),
					@ApiResponse(responseCode = "404", description = "Order not found")
			}
	)
	public Response getOrderById(
			@BeanParam AuthenticationInfo info,
			@Parameter(description = "ID of order to fetch") @PathParam("orderId") Long orderId)
			throws com.baozh.exception.NotFoundException {
		Order order = storeData.findOrderById(orderId);
		if (null != order) {
			return Response.ok().entity(order).build();
		} else {
			throw new com.baozh.exception.NotFoundException(404, "Order not found");
		}
	}

	@POST
	@Path("/order")
	@Operation(
			summary = "Place an order for a pet",
			responses = {
					@ApiResponse(content = @Content(schema = @Schema(implementation = Order.class))),
					@ApiResponse(responseCode = "400", description = "Invalid Order")
			}
	)
	public Response placeOrder(
			@BeanParam AuthenticationInfo info,
			@Parameter(description = "order placed for purchasing the pet", required = true) Order order) {
		storeData.placeOrder(order);
		return Response.ok().entity("").build();
	}

	@DELETE
	@Path("/order/{orderId}")
	@Operation(
			summary = "Delete purchase order by ID",
			responses = {
					@ApiResponse(responseCode = "400", description = "Invalid ID supplied"),
					@ApiResponse(responseCode = "404", description = "Order not found")
			}
	)
	public Response deleteOrder(
			@BeanParam AuthenticationInfo info,
			@Parameter(description = "ID of order to delete") @PathParam("orderId") Long orderId) {
		if (storeData.deleteOrder(orderId)) {
			return Response.ok().entity("").build();
		} else {
			return Response.status(Response.Status.NOT_FOUND).entity("Order not found").build();
		}
	}

	@POST
	@Path("obj")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	public Order getContentInfo(Order order) {
		return order;
	}
}
